package com.fingard.dsp.bank.directbank.payease01;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IStatement;
import com.fingard.dsp.bank.reqrespobj.statement.ReqQryStatement;
import com.fingard.dsp.bank.reqrespobj.statement.RetAtsStatement;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;

import java.util.Properties;
import java.util.Vector;

import com.jcraft.jsch.Session;

/**
 *  首信易对账单查询
 */
public class PayEase01Statement extends PayEase01Base implements IStatement {
	/**
	 * 对账文件查询
	 */
	public RetAtsStatement d_Qry_Statement(ReqQryStatement p_reqQrySttm)	throws Exception {
		/*JSch jsch = new JSch();
        
        Session session = jsch.getSession(, getBankConfig().getMapValueByKey("connectConfig", "ip"));
        session.setPassword("cmb123");
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        
        ChannelSftp channelSftp = (ChannelSftp)session.openChannel("sftp");
        channelSftp.connect();
        channelSftp.setFilenameEncoding("gbk");
        
        Vector vector  = channelSftp.ls("/");
        try{
            for(Object obj :vector){
                if(obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry){
                    String fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry)obj).getFilename();
                    System.out.println(fileName);
                }
            }
        }
        finally{
            channelSftp.quit();
            session.disconnect();
        }	
*/
		RetAtsStatement retAts = new RetAtsStatement(p_reqQrySttm);
		retAts.actNum = p_reqQrySttm.actNum;
		
		retAts.writeStart();
		String respCode = "";
		String respInfo = "";
		
		try{
			ActSetItem actItem = getBankConfig().getActSetItem(p_reqQrySttm.actNum, "");
			if (actItem == null) {
				throw new Exception("未找到账号" + p_reqQrySttm.actNum + "对应的商户配置节点");
			}
			
			//1、创建JSch类，好比是FlashFXP工具
			JSch jsch = new JSch();
			jsch.addIdentity(actItem.ownKeyStorePath);// 设置私钥 
	        
			//2、创建本次的文件传输会话对象，并连接到SFTP服务器。它好比是通过FlashFXP工具连接到SFTP服务器
			String ip = getBankConfig().getMapValueByKey("connectConfig", "ip");
			String port = getBankConfig().getMapValueByKey("connectConfig", "port");
	        Session session = jsch.getSession(actItem.merchantId, ip ,Integer.parseInt(port));
	        session.setPassword(actItem.saltKey);
	        
	        Properties config = new Properties();
	        config.put("StrictHostKeyChecking", "no");
	        
	        session.setConfig(config);
	        session.connect();
	        
	        //3、在该session会话中开启一个SFTP通道，之后就可以在该通道中进行文件传输了
	        ChannelSftp channelSftp = (ChannelSftp)session.openChannel("sftp");
	        channelSftp.connect();
	        channelSftp.setFilenameEncoding(getCharset());
	        
	        //4、进行文件传输操作：put()、get()....
	        channelSftp.put("");
	        
	        Vector vector  = channelSftp.ls("/");
	        try{
	            for(Object obj :vector){
	                if(obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry){
	                    String fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry)obj).getFilename();
	                    System.out.println(fileName);
	                }
	            }
	        } finally{
	        	//5、操作完毕后，关闭通道并退出本次会话
	            channelSftp.quit();
	            session.disconnect();
	        }
			
	        
	        /*// 解析银行返回
	        if(tmpStrRet[0].length() == 0) {
	        	WriteBankLogLn("111银行返回的报文为：" + tmpStrRet[1]);
				String respXml = Base64Helper.getFromBase64(tmpStrRet[1].split(",")[0], getCharset());
				
				WriteBankLogLn("银行返回的报文为：" + respXml);
				
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(respXml, tmpSbMsg);
				
	        	if(tmpBkReader != null) {
	        		String tmpRespCode = tmpBkReader.getFirstText("/Response/Head/Code"); 
	        		
	        		if (tmpRespCode.equals("2000")) {
	        			respCode = "0";
		        		respInfo = "";
		        		
						retAts.busDate = Format.Date10Format.format(p_reqQrySttm.getBusDate());
		        		retAts.totalNum = tmpBkReader.getFirstText("/Response/Head/TotalCount");
		        		
		        		if (tmpBkReader.read("/Response/Body/Tx")) {
		        			while(tmpBkReader.read("/Response/Body/Tx")) {
		        				RetAtsStatementDetail p_retDetail = new RetAtsStatementDetail();
		        				
		        				p_retDetail.askNumber = tmpBkReader.readedXElement.getSingleText("TxSn");
		        				p_retDetail.amount = tmpBkReader.readedXElement.getSingleText("TxAmount");
		        				p_retDetail.busType = tmpBkReader.readedXElement.getSingleText("TxType");
		        				p_retDetail.fee = tmpBkReader.readedXElement.getSingleText("InstitutionFee");
		        				p_retDetail.transMsg = tmpBkReader.readedXElement.getSingleText("Remark");
		        				p_retDetail.transState = TransState.Success;
				        		
				        		retAts.writeRetDetail(p_retDetail);
		        			}
		        		}else {
		        			respCode = "-1";
		        			respInfo = "本日未产生交易或本日对账单尚未生成";
		        		}
	        		}else {
	        			respCode = "-1";
	        			respInfo = tmpBkReader.getFirstText("/Response/Head/Message");
	        		}
	        	}else {
	        		respCode = "-1";
	        		respInfo = tmpSbMsg.toString();
	        	}
	        }else{
	        	respCode = "-1";
        		respInfo = tmpStrRet[1];
	        }
	        
	        if (!respCode.equals("0")) {
	        	retAts.writeRespCode(respCode);
	        	retAts.writeRespInfo(respInfo);
	        }*/
		}catch (Exception e) {
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(e.getMessage());
			this.WriteBankLogLn(e);
		}
		
		retAts.writeEnd();
		return retAts;
	}
}
